#include "Line.h"

// A structure for our custom vertex type
struct Vertex
{
    FLOAT x, y, z, rhw;
    DWORD color;
    static CONST DWORD FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;
};

Line::Line(Graphic& graphic) 
{
    HRESULT hresult;
    DWORD col = D3DCOLOR_XRGB(255, 255, 255);

    // Fill in our structure to draw an object.
    // x, y, z, rhw, color.
    Vertex objData[] =
    {
       { 420.0f, 150.0f, 0.5f, 1.0f, col, },
       { 420.0f, 350.0f, 0.5f, 1.0f, col, },
       { 220.0f, 150.0f, 0.5f, 1.0f, col, },
       { 220.0f, 350.0f, 0.5f, 1.0f, col, },
    };

    // Create the vertex buffer.
    hresult = GetDevice(graphic)->CreateVertexBuffer(
        sizeof(objData),
        0,
        Vertex::FVF,
        D3DPOOL_DEFAULT,
        &_VertexBuffer,
        nullptr
    );
    if (FAILED(hresult))
        throw std::runtime_error("");

    // Fill the vertex buffer.
    void* ptr;
    hresult = _VertexBuffer->Lock(0, sizeof(objData), (void**)&ptr, 0);
    if (FAILED(hresult))
        throw std::runtime_error("");

    memcpy(ptr, objData, sizeof(objData));

    _VertexBuffer->Unlock();
}

Line::~Line() 
{

}

VOID Line::Draw(Graphic& graphic) 
{
    // Render object.
    GetDevice(graphic)->SetStreamSource(0, _VertexBuffer.Get(), 0, sizeof(Vertex));
    GetDevice(graphic)->SetFVF(Vertex::FVF);
    GetDevice(graphic)->DrawPrimitive(D3DPT_LINELIST, 0, 2);
}
